package com.webserver.core;

import com.webserver.http.EmptyRequestException;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.IOException;
import java.net.Socket;

public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            //1 解析请求
            HttpServletRequest request=new HttpServletRequest(socket);
            HttpServletResponse response=new HttpServletResponse(socket);
            //2 处理请求
            DispatcherServlet servlet=new DispatcherServlet();
            servlet.service(request,response);
            //3 发起响应
            response.response();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EmptyRequestException e) {
            //e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
